#!/usr/bin/python
# -*-coding:utf-8-*-
from urllib.error import HTTPError, URLError
from urllib.request import urlopen
from urllib.request import Request
from urllib import parse
from bs4 import BeautifulSoup as bs
import pymysql.cursors
import os.path


class proxy_server_info:
    def __init__(self, ip=None, port=None, anonymous_degrees=None, type=None, location=None, response_speed=None,
                 final_verification_time=None):
        self.ip = ip
        self.port = port
        self.anonymous_degrees = anonymous_degrees
        self.type = type
        self.location = location
        self.response_speed = response_speed
        self.final_verification_time = final_verification_time


def user_proxy(proxy_addr, url):
    """
    使用代理服务器获取网页信息
    :param proxy_addr: 服务器地址
    :param url: 需要访问的网页
    :return:
    """
    import urllib.request
    proxy = urllib.request.ProxyHandler({'http': proxy_addr})
    opener = urllib.request.build_opener(proxy, urllib.request.HTTPHandler)
    urllib.request.install_opener(opener)
    data = urllib.request.urlopen(url).read().decode('utf-8')
    return data


def test_proxy(psi, url):
    try:
        proxy_addr = "%s:%s" % (psi.ip, psi.port)
        page = user_proxy(proxy_addr, url)
        soup = bs(page, "html.parser")
        # print(soup)
        return True
    except HTTPError as http:
        # print("%s:%s %s 不能连接！" % (psi.ip, psi.port, http))
        return False
    except URLError as url:
        # print("%s:%s %s 不能连接！" % (psi.ip, psi.port, url))
        return False


if __name__ == "__main__":
    for x in range(1, 2):
        url = "http://www.ip3366.net/?stype=1&page=%s" % x
        try:
            # 获得服务器返回的数据
            response = urlopen(url)
            # 处理数据
            page = response.read()
            soup = bs(page, "html.parser")
            psi = proxy_server_info()
            for child in soup.select("div#list table tbody tr"):
                for td in child.select("td"):
                    print(td)
        except HTTPError as http:
            print("ID：%s %s 网页获取失败：%s" % (x, url, http.msg))
